import tensorflow as tf
import os

# 使用autograph构建静态图
@tf.function
def strjoin(x,y):
    z =  tf.strings.join([x,y],separator = " ")
    tf.print(z)
    return z

# 将hello，world常量进行拼接
result = strjoin(tf.constant("hello"),tf.constant("world"))

print(result)

import datetime

# 创建日志
import os
stamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
# 定义生成文件的位置，创建时间
logdir = os.path.join('_log', os.path.basename(__file__), 'autograph', stamp)

# tensorboard相关的处理
writer = tf.summary.create_file_writer(logdir)

#开启autograph跟踪
tf.summary.trace_on(graph=True, profiler=True)

#执行autograph
result = strjoin("hello","world")

#将计算图信息写入日志
with writer.as_default():
    tf.summary.trace_export(
        name="autograph",
        step=0,
        profiler_outdir=logdir)

print('OK')
'''
#启动tensorboard
tensorboard --logdir ./data/autograph/
'''